$(function () {
    $("#jqGrid").jqGrid({
        url: '../firmware/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '唯一标识', name: 'rowId', index: 'row_id', width: 80, hidden: true},
            {label: '硬件类型', name: 'hardwareType', index: 'hardware_type', width: 80},
            {label: '固件型号', name: 'hardwareModel', index: 'hardware_model', width: 80},
            {label: '固件版本', name: 'firmwareLatestVersionCode', index: 'firmware_latest_version_code', width: 80},
            {label: '产品型号', name: 'equipmentModel', index: 'equipment_model', width: 80},
            {
                label: '下载地址',
                name: 'firmwareDownloadAddr',
                index: 'firmware_download_addr',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    return '<a href="http://gcsbucket.oss-cn-hongkong.aliyuncs.com/' + cellvalue + '"  target="_blank">下载</a>';
                }
            },
            {label: '上传时间', name: 'addTime', index: 'add_time', width: 80},
            {label: '固件描述信息', name: 'firmwareDescription', index: 'firmware_description', width: 80, hidden: true},
            {
                label: '船体升级模式）',
                name: 'isMandatoryUpgrade',
                index: 'is_mandatory_upgrade',
                width: 80,
                formatter: function (value) {
                    var result = "";
                    if (value == 0) {
                        result = '<span class="label label-success">不强制</span>';
                    }
                    if (value == 1) {
                        result = '<span class="label label-danger">强制</span>';
                    }
                    return result;
                }
            },
            {label: '合并版本号', name: 'mergeVersion', index: 'merge_version', width: 80},
            {
                label: 'PWV4遥控更新',
                name: 'updateMethod',
                index: 'update_method',
                width: 80,
                formatter: function (value) {
                    var result = "";
                    if (value == 0) {
                        result = '<span class="label label-danger">强制</span>';
                    }
                    if (value == 1) {
                        result = '<span class="label label-success">不强制</span>';
                    }
                    return result;
                },
            },
            {
                label: '状态', name: 'status', index: 'status', width: 80, formatter: function (value) {
                var result = "";
                if (value == 0) {
                    result = '<span class="label label-success">启用</span>';
                }
                if (value == 1) {
                    result = '<span class="label label-danger">停用</span>';
                }
                return result;
            },
            },
            {label: '固件大小', name: 'firmwareSize', index: 'firmware_size', width: 80, hidden: true},
            {label: '添加人', name: 'addUser', index: 'add_user', width: 80, hidden: true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });


});

$(document).ready(function () {

    new AjaxUpload('#pvw4Upload', {
        action: '../firmware/upload',
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            // if (!(extension && /^(jpg|jpeg|png|gif|bin|apk|zip|conf|patch)$/.test(extension.toLowerCase()))) {
            //     alert('只支持jpg、png、gif、bin格式的文件！');
            //     return false;
            // }
            vm.modal6 = true;
            vm.circleAdd();
        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                console.log(r);
                // vm.firmware.uploadList = r.uploadList;
            } else {
                alert(r.msg);
            }
        }
    });


    new AjaxUpload('#tempUpdate', {
        action: '../firmware/tempUpload',
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            // if (!(extension && /^(jpg|jpeg|png|gif|bin|apk|zip|conf|patch)$/.test(extension.toLowerCase()))) {
            //     alert('只支持jpg、png、gif、bin格式的文件！');
            //     return false;
            // }
            vm.modal6 = true;
            vm.circleAdd();
        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                console.log(r);
                // vm.firmware.uploadList = r.uploadList;
            } else {
                alert(r.msg);
            }
        }
    });


    new AjaxUpload('#upload', {
        action: '../sys/oss/upload',
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            // if (!(extension && /^(jpg|jpeg|png|gif|bin|apk|zip|conf|patch)$/.test(extension.toLowerCase()))) {
            //     alert('只支持jpg、png、gif、bin格式的文件！');
            //     return false;
            // }
        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                $("#downloadPath").val(r.url);
                $("#fileId").val(r.fileKey);
                alert('文件更新成功！');
            } else {
                alert(r.msg);
            }
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        modelValue: false,
        modal6: false,
        loading: true,
        percent: 0,
        styles: {
            height: 'calc(100% - 55px)',
            paddingBottom: '53px',
            position: 'static'
        },
        firmware: {}
    },
    computed: {
        color: function () {
            var color = '#2db7f5';
            if (this.percent == 100) {
                color = '#5cb85c';
            }
            return color;
        }
    },
    methods: {
        circleAdd: function () {
            var random = Math.ceil(Math.random() * 7);
            var timer = setInterval(vm.circleAdd, 5000);
            if (this.percent >= 100) {
                clearInterval(timer);
                this.modal6 = false;
                return false;
            }
            if (this.percent+random>100) {
                random = 100-this.percent;
            }
            this.percent += random;

        },
        circleMinus: function () {
            if (this.percent <= 0) {
                return false;
            }
            this.percent -= Math.ceil(Math.random() * 10);
        },
        asyncOK: function () {
            setTimeout(function () {
                    this.modal6 = false;
                },
                2000
            );
        },
        openDrawer: function () {
            this.modelValue = true;
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.firmware = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var zhDescription = $("#zhDescription").val();
            var enDescription = $("#enDescription").val();
            var jaDescription = $("#jaDescription").val();
            vm.firmware.firmwareDownloadAddr = $("#downloadPath").val();
            var isMandatoryUpgradeSelect = $("#isMandatoryUpgradeSelect").val();
            var isUseTo = $("#isUseTo").val();
            var isUpdateMethod = $("#isUpdateMethod").val();
            vm.firmware.firmwareDescription = '{"zh":"' + zhDescription + '","en":"' + enDescription + '","ja":"' + jaDescription + '"}';
            vm.firmware.isMandatoryUpgrade = isMandatoryUpgradeSelect;
            vm.firmware.updateMethod = isUpdateMethod;
            vm.firmware.rowId = $("#fileId").val();
            vm.firmware.status = isUseTo;
            console.log(vm.firmware);
            var url = vm.firmware.id == null ? "../firmware/save" : "../firmware/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.firmware),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../firmware/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../firmware/info/" + id, function (r) {
                vm.firmware = r.firmware;
                console.log(r.firmware.firmwareDescription);
                var firmwareDescription = JSON.parse(r.firmware.firmwareDescription);
                $("#zhDescription").val(firmwareDescription['zh']);
                $("#enDescription").val(firmwareDescription['en']);
                $("#jaDescription").val(firmwareDescription['ja']);
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});